package com.avertymaroc.library;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avertymaroc.app.R;
 
public class GrideAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<HashMap<String, Object>> data;
    private static LayoutInflater inflater=null;
    public static final int IMAGE = 1;
    public static final int BUTTON = 2;
    private  int type = IMAGE;
  
    // Constructor
    public GrideAdapter(Context c , ArrayList<HashMap<String, Object>> d){
        mContext = c;
        data = d;
        inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
 
    @Override
    public int getCount() {
        return data.size();
    }
 
    @Override
    public Object getItem(int position) {
        return position;
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	View vi=convertView;
    	if(convertView==null){
            if(type == BUTTON)
            	vi = inflater.inflate(R.layout.grid_button, null);
            else
            	vi = inflater.inflate(R.layout.grid_image, null);
            
    	}
    	HashMap<String, Object> d = data.get(position);
    	
    	if(type == BUTTON){
	    	ImageButton button = (ImageButton) vi.findViewById(R.id.button_grid);
	    	TextView titre = (TextView)vi.findViewById(R.id.titre);
	    	titre.setText((String)d.get(C.TITRE_BTN));
	    	button.setImageResource((Integer)d.get(C.ICON_BTN_ID));
    	}
    	else{
    		ImageView image = (ImageView) vi.findViewById(R.id.image_grid);
        	TextView titre = (TextView)vi.findViewById(R.id.titre);
        	titre.setText((String)d.get(C.TITRE_BTN));
        	image.setImageResource((Integer)d.get(C.ICON_BTN_ID));
    	}
        return vi;
    }
    
    
    public int getType(){return type;}
    public void setType(int t){type = t;}
 
}
